1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SActionListener.java,v 1.5 2002/09/05 15:41:49 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.scopemvc.core.ActionManager;
45 import org.scopemvc.core.ModelAction;
46
47 /***
48 * Connects component action event to a method in a model. <p>
49 *
50 * Let's have button <i>Save</i> and method <code>void saveData()</code> in
51 * model object, which should be called, when button is pressed. The connection
52 * can be done in following way: <pre>
53 * JButton button = new JButton("Save");
54 * SActionListener action = new SActionListener();
55 * action.setModelActionString("saveData");
56 * button.addActionListener(action);
57 * ...
58 * action.setBoundModel(myModel);
59 * </pre> </p>
60 *
61 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
62 * @created 05 September 2002
63 * @version $Revision: 1.5 $ $Date: 2002/09/05 15:41:49 $
64 */
65 public class SActionListener implements ActionListener {
66 private final static Log LOG = LogFactory.getLog(SActionListener.class);
67
68 private Object model;
69 private ModelAction modelAction;
70
71 /***
72 * Creates new SActionListener
73 */
74 public SActionListener() { }
75
76 /***
77 * Gets the bound model
78 *
79 * @return The boundModel value
80 */
81 public final Object getBoundModel() {
82 return model;
83 }
84
85 /***
86 * Sets name of method to call on model. The name is case sensitive.
87 *
88 * @param s The new modelActionString value
89 */
90 public final void setModelActionString(String s) {
91 setModelAction(new ModelAction(s));
92 }
93
94 /***
95 * Sets model object on which the method will be called.
96 *
97 * @param inModel The new boundModel value
98 */
99 public final void setBoundModel(Object inModel) {
100 model = inModel;
101 }
102
103 /***
104 * Called by component to notify, that user performed action on the
105 * component. Specified method is called on the model.
106 *
107 * @param ev TODO: Describe the Parameter
108 * @throws IllegalStateException if model action (method) is not set
109 * @see #setModelActionString(java.lang.String)
110 */
111 public void actionPerformed(ActionEvent ev) {
112 if (LOG.isDebugEnabled()) {
113 LOG.debug("actionPerformed " + ev);
114 }
115 if (model == null) {
116 LOG.warn("actionPerformed, but model is null. Action ignored");
117 return;
118 }
119 if (modelAction == null) {
120 throw new IllegalStateException("actionPerformed, but modelAction is not set.");
121 }
122 ActionManager actionManager = ActionManager.getInstance(model);
123 try {
124 actionManager.doAction(model, modelAction, null);
125 } catch (Exception ex) {
126 LOG.error("Cannot perform action on model " + model, ex);
127 }
128 }
129
130 private void setModelAction(ModelAction inAction) {
131 modelAction = inAction;
132 }
133 }
This page was automatically generated by Maven